﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DeepEarth.Client.MapControl.Layers;

namespace DeepEarth.Client.Services.MapBox
{
    public enum MapBoxModes
    {
        WorldDark,
        WorldPrint,
        WorldLight
    }

    public class MapBoxTileSource : TileSource
    {
        // Fields
        private bool _isInitialized;
        private bool _isTileDownloadStarted;
        private MapBoxModes _mapMode;
        private string _maptype;
        private EventHandler InitializeCompleted;
        private const string TilePathBase = "http://b.tile.mapbox.com/1.0.0/{maptype}/{z}/{x}/{y}.png";
 

        // Methods
        public MapBoxTileSource()
        {
            this._maptype = string.Empty;
        }

        public MapBoxTileSource(MapBoxModes mode)
        {
            this._maptype = string.Empty;
            this.MapMode = mode;
        }

        public override UIElement GetCopyright()
        {     const string logoPath = @"http://js.mapbox.com/img/mapbox.png";
            var link = new HyperlinkButton { TargetName = "_blank", NavigateUri = new Uri("http://mapbox.com/tos") };
            ToolTipService.SetToolTip(link, "Copyright NearMap");
            link.Content = new Image { Source = new BitmapImage(new Uri(logoPath)), Height = 48, Width = 48 };
            return link;
        }

        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            if (this.IsInitialized)
            {
                int num = this.TileToZoom(tileLevel);
                this._isTileDownloadStarted = true;
                tilePositionY = (int) Math.Abs((double) (tilePositionY - (Math.Pow(2.0, (double) num) - 1.0)));
                string str = "http://b.tile.mapbox.com/1.0.0/{maptype}/{z}/{x}/{y}.png";
                return new Uri(str.Replace("{maptype}", this._maptype).Replace("{z}", num.ToString()).Replace("{x}", tilePositionX.ToString()).Replace("{y}", tilePositionY.ToString()));
            }
            return null;
        }

        // Properties
        public override bool IsInitialized
        {
            get
            {
                return this._isInitialized;
            }
        }

        public MapBoxModes MapMode
        {
            get
            {
                return this._mapMode;
            }
            set
            {
                if (this._isTileDownloadStarted)
                {
                    throw new InvalidOperationException();
                }
                this._mapMode = value;
                base.Id = value.ToString();
                this._isInitialized = true;
                if (this.InitializeCompleted != null)
                {
                    this.InitializeCompleted(this, null);
                }
                switch (this.MapMode)
                {
                    case MapBoxModes.WorldDark:
                        this._maptype = "world-dark";
                        this.MinZoomLevel = 1;
                        this.MaxZoomLevel = 11;
                        break;

                    case MapBoxModes.WorldPrint:
                        this._maptype = "world-print";
                        this.MinZoomLevel = 1;
                        this.MaxZoomLevel = 9;
                        break;

                    case MapBoxModes.WorldLight:
                        this._maptype = "world-light";
                        this.MinZoomLevel = 1;
                        this.MaxZoomLevel = 11;
                        break;
                }
            }
        }

        public override Color TileColor
        {
            get
            {
                return Colors.Gray;
            }
        }
    }
}